package com.smforj.ssm.modules.wc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smforj.ssm.frame.core.enums.RouterMapping;
import com.smforj.ssm.frame.core.utils.StringEx;
import com.smforj.ssm.frame.core.web.controller.BaseControllerSupportImpl;
import com.smforj.ssm.frame.core.web.domain.DataResult;
import com.smforj.ssm.modules.lib.domain.SysReader;
import com.smforj.ssm.modules.lib.domain.SysReaderWechat;
import com.smforj.ssm.modules.lib.service.ReaderWechatService;
import com.smforj.ssm.modules.wc.service.BusinessService;
import com.smforj.ssm.modules.wc.util.JsapiUtil;

/***
 * 公共操作Controller
 * 针对微信提供 注册功能
 * 
 * @author Haijun Gao 
 * @date 2016-8-11 下午2:00:54
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */
@RouterMapping("/ssr/sys/common/")
@Controller
@RequestMapping("/wechat/register")
public class LoginController extends BaseControllerSupportImpl {
	
	private Logger log = Logger.getLogger(this.getClass());  
	@Autowired
    @Qualifier("esbusinessService")
	private BusinessService businessService;
	@Autowired
	private ReaderWechatService readerWechatService; 

	/***
	 * 跳转到注册界面
	 * @return "opQSRwWhh9sCHQejRW4V8SlhkFNI";//
	 * @date 2016-8-11 下午2:03:45
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView goRegister(HttpServletResponse response,ModelMap model)
	{   
		ModelAndView view = new ModelAndView(this.getViewPath("message"),model);
		try{ 
			Object openid = "opQSRwWhh9sCHQejRW4V8SlhkFNI";//request.getSession().getAttribute(SessionC.OPENID);
			System.out.println("openid："+openid); 
			if(openid == null || StringEx.isExEmpty(openid.toString()))  
				model.addAttribute("message", "获取用户权限失败");   
			else 
			{
				SysReaderWechat srw = readerWechatService.getReaderWechat(openid.toString());
				System.out.println("srw："+srw);
				if(srw != null) 
					model.addAttribute("message", "您已经注册过了"); 
				else
					view = new ModelAndView(this.getViewPath("register"),model);
			}
			
			JsapiUtil.setJsapiTicket(request,response,openid.toString(),model);
				
		}catch(Exception ex)
		{    
			ex.printStackTrace();
			log.error("获取用户权限失败: "+ex.getMessage());
			model.addAttribute("message", "获取用户权限失败");
		}
		System.out.println("view:"+view);
		return view;
	}   
	
	/***
	 * 注册
	 * @return
	 * @date 2016-8-11 下午2:12:06
	 */
	@RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ModelAndView doRegister(HttpServletRequest request,SysReader reader,ModelMap model)
	{ 
		log.info("doRegister"); 
		System.out.println("doRegister SysReader: "+reader); 
		ModelAndView view = new ModelAndView(this.getViewPath("message"));;
		try{
			Object openid = "opQSRwWhh9sCHQejRW4V8SlhkFNI";//request.getSession().getAttribute(SessionC.OPENID);
			System.out.println("doRegister openid: "+openid); 
			if(openid != null && !StringEx.isExEmpty(openid.toString())) 
			{ 
				DataResult result = businessService.addWechatUser(reader, openid.toString());  
				model.addAttribute("message", result.getMessage());  
			}else 
				model.addAttribute("message", "获取用户权限失败");  
		}catch(Exception ex)
		{  
			log.error("获取用户权限失败: "+ex.getMessage());
			model.addAttribute("message", "获取用户权限失败");
		}
		return view;
	}
	
	
}
